"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : ALTER PROCEDURE
Case Name   : 非系统管理员所有者修改procedure所有者后删除procedure
Create At   : 2022/05/11
Owner       : @zou_jialiang050
Description :
    1.创建用户1
    2.创建用户2
    3.用户2的权限赋给用户1
    4.用户1所在模式的create权限赋予用户2
    5.用户1创建procedure
    6.用户1修改procedure的所有者为用户2
    7.联合系统表pg_proc,pg_user查询procedure的所有者
    8.用户1删除procedure
    9.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
    7.成功
    8.成功
    9.成功
History     :
"""

import os
import unittest
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class PROCEDURE(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.user1 = 'u_alter_procedure_0028_01'
        self.user2 = 'u_alter_procedure_0028_02'
        self.procedure = 'p_alter_procedure_0028'
        self.table = 't_alter_procedure_0028'

    def test_alter_procedure(self):
        step = 'step1:创建用户1 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user1} cascade;
            create user {self.user1} with password '{macro.PASSWD_REPLACE}';
            ''')
        self.logger.info(create_user)
        self.assertIn(self.Con.DROP_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step2:创建用户2 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user2} cascade;
            create user {self.user2} with password '{macro.PASSWD_REPLACE}';
            ''')
        self.logger.info(create_user)
        self.assertIn(self.Con.DROP_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step3:将用户2的权限赋予用户1 expect:成功'
        self.logger.info(step)
        grant_pri = self.primary_sh.execut_db_sql(
            f'''grant {self.user2} to {self.user1};''')
        self.logger.info(grant_pri)
        self.assertIn(self.Con.GRANT_SUCCESS_MSG, grant_pri, '执行失败' + step)

        step = 'step4:用户1所在模式的create权限赋予用户2 expect:成功'
        self.logger.info(step)
        grant_pri = self.primary_sh.execut_db_sql(
            f'''grant create on schema {self.user1} to {self.user2};''')
        self.logger.info(grant_pri)
        self.assertIn(self.Con.GRANT_SUCCESS_MSG, grant_pri, '执行失败' + step)

        step = 'step5:用户1创建procedure expect:成功'
        self.logger.info(step)
        create_pro = self.primary_sh.execut_db_sql(
            f'''create or replace procedure {self.procedure}()
            is
            var1 int:= 1;
            var2 int:= 2;
            begin
                drop table if exists {self.table};
                create table {self.table}(c_int int);
                insert into {self.table} values(var1),(var2);
            end;''', sql_type=f" -U {self.user1} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(create_pro)
        self.assertIn(self.Con.CREATE_PROCEDURE_SUCCESS_MSG, create_pro,
                      '执行失败' + step)

        step = 'step6:用户1修改procedure的所有者为用户2 expect:成功'
        self.logger.info(step)
        alter_procedure = self.primary_sh.execut_db_sql(
            f'''alter procedure {self.procedure}() owner to {self.user2};''',
            sql_type=f" -U {self.user1} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(alter_procedure)
        self.assertIn(self.Con.ALTER_FUNCTION_SUCCESS_MSG, alter_procedure,
                      '执行失败' + step)

        step = 'step7:联合系统表pg_proc,pg_user查询procedure的所有者 ' \
               'expect:返回用户2'
        self.logger.info(step)
        select_usename = self.primary_sh.execut_db_sql(
            f'''select usename from pg_user where usesysid = 
            (select proowner from pg_proc where proname = '{self.procedure}');
            ''')
        self.logger.info(select_usename)
        self.assertIn(self.user2, select_usename, '执行失败' + step)

        step = 'step8:用户1删除procedure expect:成功'
        self.logger.info(step)
        drop_procedure = self.primary_sh.execut_db_sql(
            f'''drop procedure {self.procedure};''', sql_type=f" -U "
            f"{self.user1} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(drop_procedure)
        self.assertIn(self.Con.DROP_PROCEDURE_SUCCESS_MSG, drop_procedure,
                      '执行失败' + step)

    def tearDown(self):
        step = 'step9:清理环境 expect:清理环境成功'
        self.logger.info(step)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop user {self.user1} cascade;
            drop user {self.user2} cascade;''')
        self.logger.info(clean_environment)
        self.assertEqual(
            clean_environment.count(self.Con.DROP_ROLE_SUCCESS_MSG), 2,
            '执行失败' + step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
